package com.stox.ui.javafx.core.model;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

import com.stox.design.Callback;
import com.stox.dto.AbstractModel;
import com.stox.dto.intf.AbstractModelSearchable;
import com.stox.ui.javafx.core.util.TreeUtil;

public class TreeViewAbstractModelSearchable implements AbstractModelSearchable {
	private static final long serialVersionUID = 1L;

	private TreeView<AbstractModel> treeView;
	
	public TreeViewAbstractModelSearchable() {
		
	}
	
	public TreeViewAbstractModelSearchable(TreeView<AbstractModel> treeView) {
		this.treeView = treeView;
	}
	
	public void setTreeView(TreeView<AbstractModel> treeView) {
		this.treeView = treeView;
	}
	
	@Override
	public List<AbstractModel> get() {
		List<TreeItem<AbstractModel>> treeItems = treeView.getSelectionModel().getSelectedItems();
		List<AbstractModel> models = new ArrayList<AbstractModel>();
		for(TreeItem<AbstractModel> treeItem : treeItems){
			models.add(treeItem.getValue());
		}
		return models;
	}

	@Override
	public AbstractModel search(Callback<AbstractModel, Boolean> matcher, int start,boolean forward) {
		return TreeUtil.search(treeView, matcher, start, forward);
	}
	
	@Override
	public AbstractModel next(boolean forward) {
		return TreeUtil.getNextValue(treeView, forward);
	}

	public TreeView<AbstractModel> getTreeView() {
		return treeView;
	}

}
